package org.zfes.snowier.cloud.zbss.cms.words.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.words.service.ICmsWordsHotService;
import org.zfes.snowier.cloud.zbss.cms.words.dao.CmsWordsHotMapper;
import org.zfes.snowier.cloud.zbss.cms.words.model.CmsWordsHot;
import java.util.stream.Stream;
@Service
public class CmsWordsHotServiceImpl implements ICmsWordsHotService{
 	@Autowired
	private CmsWordsHotMapper cmsWordsHotMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsWordsHot(CmsWordsHot cmsWordsHot) {
		ZBeanUtil.setCreateInfo(cmsWordsHot);
		ZBeanUtil.validateBean(cmsWordsHot);
		cmsWordsHotMapper.insertSelective(cmsWordsHot);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsWordsHot> loadCmsWordsHotById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsWordsHotMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsWordsHot(CmsWordsHot cmsWordsHot) {
		ZBeanUtil.validateBean(cmsWordsHot);
		//检查是否已存在该热词
		if(selectByWords(cmsWordsHot.getWords()).isPresent()) {//已存在
			ZAlert.serviceLogicalError("该热词已存在！");
		}
		cmsWordsHot.setUpdateTime(ZDateUtil.newDate());
		cmsWordsHotMapper.updateByPrimaryKeySelective(cmsWordsHot);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsWordsHot(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsWordsHotMapper.updateStatus(id, Byte.valueOf("-1"),ZDateUtil.newDate());
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsWordsHotStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsWordsHotMapper.updateStatus( id,  status,ZDateUtil.newDate()) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsWordsHotDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("words").ifPresent(v->pm.updateParam("words", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(cmsWordsHotMapper.selectListCount(pm), cmsWordsHotMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	//查看你是否已存在该热词
	public Optional<CmsWordsHot> selectByWords(String words) {
		if(!words.isEmpty()){
			return Optional.ofNullable(cmsWordsHotMapper.selectByWords(words));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	//查看某站点热词
	public List<CmsWordsHot> loadCmsWordsHotBySiteId(Long sideId) {
		List<CmsWordsHot> cmsWordsHotList=cmsWordsHotMapper.loadCmsWordsHotBySiteId(sideId);
		return cmsWordsHotList;
	}
	
}